﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Presenters;
using System.Collections;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class TestEmail : TemplateForm, ISendMailView
    {
        private PresenterSendMail send;
        private String[] listEmail = new String[152];
        private int index = 0;
        public TestEmail()
        {
            InitializeComponent();
            send = new PresenterSendMail(this);
        }
        public TestEmail(string email)
        {
            InitializeComponent();
            send = new PresenterSendMail(this);
            this.txtFrom.Text = email;
            this.bSelectEmail.Enabled = false;
        }
        #region ISendMailView Members

        public string From
        {
            get { return listEmail[index]; }
        }

        public string Password
        {
            get { return "romerrios"; }
        }

        public string ToEmail
        {
            get { return "roemerdlr@gmail.com"; }
        }

        public string Message
        {
            get { return txtMessage.Text; }
        }

        public string Subject
        {
            get { return txtSubject.Text; }
        }

        public string File
        {
            get { return txtFile.Text.Trim(); }
        }

        #endregion

        #region IBaseView Members

        public string MessageWarnings
        {
            set {
                    if (!string.Equals(value,"All inputs are required.") && !string.Equals(value,"the format the email is incorrect") && !string.Equals(value,"error in send email"))
                    {
                        if (index==0)
                        {
                            ClearAll();
                        }
                    }
                    MessageBox.Show(value); 
                }
            get { return "never"; }
        }

        #endregion

        private void bSelectEmail_Click(object sender, EventArgs e)
        {
            ListEmail listMail = new ListEmail();
            listMail.ShowDialog();
            
            foreach (string item in listMail.GoEmailSend())
            {
                listEmail[index] = item;
                ++index;
                txtFrom.Text = txtFrom.Text + "; " + item;
            }
            if (!string.IsNullOrEmpty(txtFrom.Text))
            {
                this.txtFrom.Enabled = false;   
            }
        }

        private void bSend_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSubject.Text) || string.IsNullOrEmpty(txtMessage.Text) ||string.IsNullOrEmpty(txtFrom.Text))
            {
                MessageBox.Show("All inputs are required.");
            }
            else
            {
                if (index != 0)
                {
                    int count = index;
                    index = 0;
                    for (int i = 0; i < count; i++)
                    {
                        send.SendNewEmail();
                        index++;
                    }
                    this.txtFrom.Enabled = true;
                    ClearAll();
                }
                else
                {
                    listEmail[index] = txtFrom.Text;
                    send.SendNewEmail();
                    if (!this.bSelectEmail.Enabled)
                    {
                        this.bSelectEmail.Enabled = true;
                    }
                }
            }
        }
        private void ClearAll() 
        {
            this.txtFrom.Text = string.Empty;
            this.txtMessage.Text = string.Empty;
            this.txtSubject.Text = string.Empty;
        }
        
        private void bCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Browser_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "Select file for send";
            if (open.ShowDialog() == DialogResult.OK)
            {
                txtFile.Text = open.FileName;
                FileInfo file = new FileInfo(open.FileName);
                if (file.Length>5242880)
                {
                    MessageBox.Show("please select a file size less than 5Mb");
                    this.txtFile.Text = "";
                }
            }
        }
    }
}
